﻿using System;
using COM.DTO;
using NHibernate;
using COM.Logging;
using NHibernate.Criterion;

namespace DAL.DAO
{
    class AddressTypeDAO
    {
        public static void Add(AddressType addressType)
        {
            {
                using (ISession session = NHibernateHelper.OpenSession())
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.SaveOrUpdate(addressType);
                    transaction.Commit();
                }
            }
        }
        public static void Update(AddressType addressType)
        {
            {
                using (ISession session = NHibernateHelper.OpenSession())
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Update(addressType);
                    transaction.Commit();
                }
            }
        }

        public static void Remove(AddressType addressType)
        {
            {
                using (ISession session = NHibernateHelper.OpenSession())
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Delete(addressType);
                    transaction.Commit();
                }
            }
        }

        public static AddressType GetAddressTypeById(String addressTypeId)
        {
            using (ISession session = NHibernateHelper.OpenSession())
                return session.Get<AddressType>(addressTypeId);
        }
        public static AddressType GetAddressTypeByName(string addressTypeName)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                var addressType = session
                    .CreateCriteria(typeof(AddressType))
                    .Add(Restrictions.Eq("AddressTypeName", addressTypeName))
                    .UniqueResult<AddressType>();
                return addressType;
            }

        }
    }
}
